package com.hz.config;

import com.hz.authentication.CustomSuccessHandler;
import com.hz.authentication.LoginSuccessHandler;
import com.hz.authentication.SpringAuthenticationProvider;
import com.hz.authentication.SpringDataUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.web.access.AccessDeniedHandler;

/**
 * Created by kaddddd on 2018/5/8 0008.
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    //定义自定义身份验证机制(AuthenticationManagerBuilder不存在的情况)
//    @Bean
//    public SpringAuthenticationProvider springAuthenticationProvider() {
//        return new SpringAuthenticationProvider();
//    }
    @Autowired
    private AccessDeniedHandler accessDeniedHandler;

    @Autowired
    CustomSuccessHandler customSuccessHandler;

    @Bean
    public static NoOpPasswordEncoder passwordEncoder() {
        return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
    }


    //定义自定义身份验证(这只用于 AuthenticationManagerBuilder 不存在，并且没有 AuthenticationProviderBean 被定义的情形！)
    @Bean
    public SpringDataUserDetailsService springDataUserDetailsService() {
        return new SpringDataUserDetailsService();
    }

//    @Override
//    public AuthenticationManager authenticationManagerBean() throws Exception {
//        return super.authenticationManagerBean();
//    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        //注入userDetailsService，需要实现userDetailsService接口
        //auth.userDetailsService(userDetailsService);
        auth.userDetailsService(springDataUserDetailsService());
        //内存认证
//        auth.inMemoryAuthentication()
//                .withUser("zht").password("123").roles("REQ").and()
//                .withUser("xk").password("123").roles("CHECKER").and()
//                .withUser("admin").password("123").roles("ADMIN");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/widget").permitAll()
                .antMatchers("/admin/**").hasAnyRole("ADMIN")
                .antMatchers("/user/**").hasAnyRole("REQ")
                .antMatchers("/checker/**").hasAnyRole("CHECKER")
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .permitAll().successHandler(customSuccessHandler)
                .and()
                .logout()
                .permitAll()
                .and()
                .exceptionHandling().accessDeniedHandler(accessDeniedHandler);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        //可以忽略静态资源
        web.ignoring().antMatchers("static/js/**");
    }
}